package org.socialresume.client.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;

public class Feed implements Serializable{
	
	private static final long serialVersionUID = -1254322391524512391L;
	
	private FeedTypeEnum type;
	private String logoUrl;
	private String title;
	private String url;
	private String description;
	private Set<FeedItem> updates;
	private ValueChangeHandler<Collection<FeedItem>> handler;
	
	public Feed(){}
	
	public Feed(FeedTypeEnum type, String logoUrl, String title, String description, String link){
		this.type = type;
		this.logoUrl = logoUrl;
		this.url = link;
		this.title = title;
		this.description = description;
	}
	
	public void setUpdates(Collection<FeedItem> items){
		this.updates = new HashSet<FeedItem>();
		if(items !=null){
			this.updates.addAll(items);
		}
		if(handler !=null){
			ValueChangeEvent<Collection<FeedItem>> event = new ValueChangeEvent<Collection<FeedItem>>(updates){};
			handler.onValueChange(event);
		}
	}
	
	public FeedTypeEnum getType() {
		return type;
	}

	public String getTitle() {
		return title;
	}

	public Set<FeedItem> getUpdates() {
		return updates;
	}
	
	public String getDescription() {
		return description;
	}
	
	public String getLogoUrl() {
		return logoUrl;
	}

	public String getUrl() {
		return url;
	}

	public void addValueChangeHandler(
			ValueChangeHandler<Collection<FeedItem>> handler) {
		this.handler =handler;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Feed other = (Feed) obj;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}
}
